/**
 * Licensed to Inspektr under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Inspektr licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a
 * copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.sunkey.log.audit.spi.support;

import java.util.Collection;
import java.util.Iterator;

import org.aspectj.lang.JoinPoint;

import com.sunkey.log.audit.annotation.Audit;
import com.sunkey.log.audit.spi.AuditResourceResolver;
import com.sunkey.log.util.JsonUtil;

/**
 * Implementation of {@link com.sunkey.log.audit.spi.AuditResourceResolver} that uses the toJson version of the return
 * value
 * as the resource.
 * 
 * @author sunkey
 * @date Aug 23, 2014
 */
public final class ReturnValueAsJSONResourceResolver implements AuditResourceResolver {

    @SuppressWarnings("rawtypes")
    public String[] resolveFrom(final JoinPoint auditableTarget, final Object retval) {
        if (retval == null) {
            return new String[0];
        }
        if (retval instanceof String) {
            return new String[] { retval.toString() };
        }
        if (retval instanceof Collection) {
            final Collection c = (Collection) retval;
            final String[] retvals = new String[c.size()];

            int i = 0;
            for (final Iterator iter = c.iterator(); iter.hasNext() && i < c.size(); i++) {
                final Object o = iter.next();

                if (o != null) {
                    retvals[i] = JsonUtil.toJson(iter.next());
                }
            }

            return retvals;
        }

        if (retval instanceof Object[]) {
            final Object[] vals = (Object[]) retval;
            final String[] retvals = new String[vals.length];
            for (int i = 0; i < vals.length; i++) {
                retvals[i] = JsonUtil.toJson(vals[i]);
            }

            return retvals;
        }
        return new String[] { JsonUtil.toJson(retval) };
    }

    public String[] resolveFrom(final JoinPoint auditableTarget, final Exception exception) {
        final String message = exception.getMessage();
        if (message != null) {
            return new String[] { message };
        }
        return new String[] { exception.toString() };
    }

    @Override
    public String[] resolveFrom(JoinPoint target, Object returnValue, Audit audit) {
        return resolveFrom(target, returnValue);
    }

    @Override
    public String[] resolveFrom(JoinPoint target, Exception exception, Audit audit) {
        return resolveFrom(target, exception);
    }
}
